package ru.cod.rs.main.units;

import ru.cod.rs.main.types.LevelPreferences;
import ru.cod.rs.main.units.parts.Body;
import ru.cod.rs.main.units.parts.Chassis;
import android.graphics.Canvas;
import android.view.SurfaceView;

public abstract class LiveUnit extends AbstractUnit implements Moveable {

	protected Body body;
	protected Chassis chassis;

	protected LevelPreferences preferences;

	// //////////////////////////////////////////////////////////
	public LiveUnit(LevelPreferences cntx) {
		super(cntx.getView());
		preferences = cntx;
		y = cntx.getGround();
		setParts(cntx.getView());

		chassis.setCoords(x, y);
		body.setCoords(chassis.getX(), chassis.getY());
	}

	// //////////////////////////////////////////////////////////
	@Override
	public void render(Canvas canvas) {
		body.render(canvas);
		chassis.render(canvas);
	};

	@Override
	public void update() {
		chassis.update();
		setCoords(chassis.x + chassis.getWidth() / 2,
				chassis.y + chassis.getHeight());
		body.setCoords(chassis.getX(), chassis.getY());
	}

	// //////////////////////////////////////////////////////////
	/**
	 * @return the body
	 */
	public Body getBody() {
		return body;
	}

	/**
	 * @param body
	 *            the body to set
	 */
	public void setBody(Body body) {
		this.body = body;
	}

	/**
	 * @return the chassis
	 */
	public Chassis getChassis() {
		return chassis;
	}

	/**
	 * @param chassis
	 *            the chassis to set
	 */
	public void setChassis(Chassis chassis) {
		this.chassis = chassis;
	}

	protected abstract void setParts(SurfaceView cntx);

	/*
	 * @see ru.cod.rs.main.units.AbstractUnit#getHeight()
	 */
	@Override
	public int getHeight() {
		return body.getHeight() + chassis.getHeight();
	}

	@Override
	public void setCoords(int x, int y) {
		setX(x);
		setY(y);
		chassis.setCoords(x, y);
		body.setCoords(x, y);
	}

	// /////////////////////////////////////////////////////////

	@Override
	public void moveLeft() {
		chassis.moveL();
		chassis.setxDestination(0);
	}

	@Override
	public void moveRight() {
		chassis.moveR();
		chassis.setxDestination(preferences.getRightEnd());
	}

	@Override
	public void stop() {
		chassis.stop();
	}

}
